package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.enums.DubboServiceEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.util.ChangeDubboUtil;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.momp.facade.request.MchntJoinJointMcRequest;
import cc.rengu.igas.momp.facade.response.MchntJoinJointMcResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * @author Xuegui Cao
 * @date 2020/7/1
 * @description 商户参与联和营销活动
 */
public class MchntJoinJointMcService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntJoinJointMcRequest request = new MchntJoinJointMcRequest();
        // 读取存在xml树里的实体类
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntJoinJointMcRequest dubboRequest = (MchntJoinJointMcRequest) request;
        dubboRequest.setQueryOrgId(ChangeDubboUtil.getSignInstId());

        //修改请求Header
        ChangeDubboUtil.changeHeader(dubboRequest.getHeader(), DubboServiceEnum.M00MO028.getTxnNum());

        //调用Dubbo，获取结果集和相应对象。
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntJoinJointMcResponse> result = (Result<MchntJoinJointMcResponse>) dubboService.callDubboService(DubboServiceEnum.M00MO028.getServerName(), DubboServiceEnum.M00MO028.getMethodName(), dubboRequest);
        if (result != null && result.isSuccess()) {
            //调用成功
            BizResponse<MchntJoinJointMcResponse> bizResponse = new BizResponse<>();
            ChangeDubboUtil.setBizResponse(bizResponse, AppParamConstant.SYS_ID, result);
            return bizResponse;
        } else {
            rglog.error("【商户服务】调用【{}】服务异常！", DubboServiceEnum.M00MO028.getDescription());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntJoinJointMcResponse modelRespose = (MchntJoinJointMcResponse) bizResponse.getResult();
        ConvertUtil.convertInput(modelRespose);
    }
}